import * as tf from '@tensorflow/tfjs';

export function preprocessData(data, sequenceLength) {
  // Determine the number of unique classes (0-9)
  const numClasses = 10;
  
  // Create sequences and corresponding next numbers
  const sequences = [];
  const nextNumbers = [];
  
  for (let i = 0; i < data.length - sequenceLength; i++) {
    // Ensure each sequence is an array of arrays (shape [sequenceLength, 1])
    const sequence = data.slice(i, i + sequenceLength).map(num => [num]);
    sequences.push(sequence);
    nextNumbers.push(data[i + sequenceLength]);
  }
  
  // Convert to tensors
  const X = tf.tensor3d(
    sequences,
    [sequences.length, sequenceLength, 1]
  );
  
  const y = tf.oneHot(
    tf.tensor1d(nextNumbers, 'int32'),
    numClasses
  );
  
  return { X, y, numClasses };
}

export function generateRandomSequence(length) {
  return Array.from({ length }, () => Math.floor(Math.random() * 10));
}